﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using OpenGado.DATA.Entities;
using NHibernate.Util;

namespace OpenGado.DATA
{
 /// <summary>
 /// Class controls the crud operations of Purchase order items and purchase order tables. 
 /// </summary>
    public class PurchaseOrderDataControl
    {
        private static PurchaseOrderDataControl purchaseOrderDataControl;
        private static object lockPurchaseOrder = new object();

        private ISession session;

        /// <summary>
        /// Handles session opening.
        /// </summary>
     public ISession Session
     {
         get
         {
             session = FactoryHandler.CreateFactory().OpenSession();
             return session;
         }
     }
        /// <summary>
        /// Handles instance creation for this class
        /// </summary>
     public static PurchaseOrderDataControl Instance
     {
         get
         {
             lock (lockPurchaseOrder)
             {
                 if (purchaseOrderDataControl ==null)
                 {
                     purchaseOrderDataControl = new PurchaseOrderDataControl();
                 }

             }
             return purchaseOrderDataControl;
         }
     }
        /// <summary>
        /// Saves a complete purchase order head and its items.
        /// </summary>
        /// <param name="purchaseOrder"></param>
        /// <param name="purchaseItems"></param>
        public void Save(PurchaseOrders purchaseOrder, List<PurchaseOrderItems> purchaseItems)
        {
            ISession tsession = Session;
            ITransaction transaction = tsession.BeginTransaction();

            try
            {
                tsession.SaveOrUpdate(purchaseOrder);

                
                if (purchaseItems != null)
                    foreach (PurchaseOrderItems i in purchaseItems)
                    {
                        tsession.SaveOrUpdate(i);
                    }

                transaction.Commit();
            }
            catch (Exception)
            {

                transaction.Rollback();
            }
            finally
            {
                tsession.Close();
            }

        }
        /// <summary>
        /// Saves purchase order item object
        /// </summary>
        /// <param name="purchaseOrderItems"></param>
        public void Save(PurchaseOrderItems purchaseOrderItems)
        {
            ISession tsession = Session;
            ITransaction transaction = tsession.BeginTransaction();

            try
            {
                tsession.SaveOrUpdate(purchaseOrderItems);
                transaction.Commit();
            }
            catch (Exception)
            {

                transaction.Rollback();
            }
          

            tsession.Close();        
        }
        /// <summary>
        /// Saves purchase orders
        /// </summary>
        /// <param name="purchaseOrders"></param>
        public void Save(PurchaseOrders purchaseOrders)
        {
            ISession tsession = Session;
            ITransaction transaction = tsession.BeginTransaction();

            try
            {
                tsession.SaveOrUpdate(purchaseOrders);
                transaction.Commit();
            }
            catch (Exception)
            {

                transaction.Rollback();
            }

            tsession.Close();        

        }
        public IList<PurchaseOrders> GetAllPurchaseOrders()
        {
      
               ISession tsession = Session;
             
                ICriteria criteria = tsession.CreateCriteria<PurchaseOrders>();
                IList<PurchaseOrders> purchaseOrders = criteria.List<PurchaseOrders>();
                tsession.Close();


            return purchaseOrders;
        }
        /// <summary>
        /// Returns all pruchase orders filtered by status
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public IList<PurchaseOrders> GetAllPurchaseOrders(String status)
        {

            ISession tsession = Session;

            ICriteria criteria = tsession.CreateCriteria<PurchaseOrders>();
            criteria.Add(Restrictions.Eq("PoStatus", status));

            IList<PurchaseOrders> purchaseOrders = criteria.List<PurchaseOrders>();

           
            tsession.Close();

          
            return purchaseOrders;
        }
        public PurchaseOrders GetPurchaseOrderById(int purchaseOrderId)
        {
            ISession tsession = Session;

            var purchaseOrder = tsession.Get<PurchaseOrders>(purchaseOrderId);
          
            NHibernateUtil.Initialize(purchaseOrder.Vendor);
            NHibernateUtil.Initialize(purchaseOrder.PurchaseOrderItems);
            
            tsession.Close();

            return purchaseOrder;
        }

        /// <summary>
        /// Returns purchase order items related to a particular purchase order.
        /// </summary>
        /// <param name="purchaseOrder"></param>
        /// <returns></returns>
        public IList<PurchaseOrderItems> GetAllPurchaseOrderItems(PurchaseOrders purchaseOrder)
        {
            ISession tsession = Session;

            ICriteria criteria = tsession.CreateCriteria<PurchaseOrderItems>();
            criteria = criteria.CreateCriteria("PurchaseOrders");
            if (purchaseOrder != null)
            {
                criteria.Add(Restrictions.Eq("PurchaseOrders", purchaseOrder.PurchaseOrderId));
            }

            var purchaseOrderItems = criteria.List<PurchaseOrderItems>();

            tsession.Close();

            return purchaseOrderItems;
        }
       
        /// <summary>
        /// Deletes Purchase order object
        /// </summary>
        /// <param name="p"></param>
        public void Delete(PurchaseOrders p)
        {
            ISession tsession = Session;
            ITransaction transaction = tsession.BeginTransaction();

            try
            {
                tsession.Delete(p);

                transaction.Commit();
            }
            catch (Exception)
            {
                transaction.Rollback();
                throw;
            }
            tsession.Close();
        }

        
        /// <summary>
        /// Deletes Purchase order item object
        /// </summary>
        /// <param name="p"></param>
        public void Delete(PurchaseOrderItems p)
        {
            ISession tsession = Session;
            ITransaction transaction = tsession.BeginTransaction();

            try
            {
                
                tsession.Delete(p);
                transaction.Commit();
            }
            catch (Exception)
            {
                transaction.Rollback();
                throw;
            }

            tsession.Close();
        }
        /// <summary>
        /// Generates teh next order id
        /// </summary>
        /// <returns></returns>
     public int GetNextId()
        {
            int nextId = 0;


            ISession tsession = Session;
            IQuery query = tsession.CreateSQLQuery("select max(purchase_order_id) from purchase_orders");
            nextId = (int) query.UniqueResult();
            nextId++;
            return nextId;
        }
    }
}
